package com.game.server.message.proto.execute;

import com.comm.ISession;

import com.game.majiang.config.DataManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.server.db.dao.XyGameLogDao;
import com.game.server.db.entity.UserInfoData;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.db.entity.XyUnionInfo;
import com.game.server.message.proto.in.AbstractXYfindGameMinLogReq;
import com.game.server.message.proto.out.XYfindGameAllLogRsp;
import com.game.server.message.proto.out.XYfindGameMinLogRsp;
import com.game.server.message.proto.out.XYgameMinlogStruct;
import com.game.server.util.CompuUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.math.BigDecimal;

public class XYfindGameMinLogReq extends AbstractXYfindGameMinLogReq {

    @Override
    public void Execute(ISession session) {
        LogicClient client = (LogicClient) LogicServer.getInstance().getClient(session.getId());
        if (client != null&&client.checkClick(msgcode)) {
            XYfindGameMinLogRsp rsp = new XYfindGameMinLogRsp();
            rsp.setType(this.type);
            JSONObject tabinfo = XyGameLogDao.findTableId(this.tableonly,this.type);
            if (tabinfo!=null && tabinfo.size()>0){
                rsp.setTableid(tabinfo.getInt("tableid"));
                rsp.setTableName(tabinfo.getString("tablename"));
                rsp.setGameDuration(tabinfo.getInt("gametime"));
                int zongshoushu = 0;
                int zongdairu = 0;
                int clubzongdairu = 0;
                int clubjifen = 0;
                int clubfenpei = 0;
                JSONArray list = tabinfo.getJSONArray("list");
                JSONArray guotaList = tabinfo.getJSONArray("guotaList");
                XYgameMinlogStruct[] minlogStructs = new XYgameMinlogStruct[list.size()];
                int index = 0;
                for (Object ent : list) {
                    XYgameMinlogStruct minlogStruct = new XYgameMinlogStruct();
                    JSONObject entObj = JSONObject.fromObject(ent);
                    int dairu = entObj.getInt("dairu");
                    int cid = entObj.getInt("cid");
                    int win = entObj.getInt("win");
                    int shoushu = entObj.getInt("shoushu");
                    UserInfoData userinfo = DataManager.getInstance().getMapUserData().get(entObj.getInt("uid"));
                    minlogStruct.setDairu(entObj.getInt("dairu"));
                    minlogStruct.setUserid(userinfo.getId());
                    minlogStruct.setUserhead(userinfo.getHead());
                    minlogStruct.setUsername(userinfo.getName());
                    minlogStruct.setWin(win);
                    minlogStruct.setFenpei(0);
                    if (this.type>1){
                        if (this.type==2){
                            XyUnionInfo unionInfo = DataManager.getInstance().getMapUnion().get(this.unionid);
                            XyClubInfo clubInfo = DataManager.getInstance().getMapClub().get(this.clubid);
                            if (unionInfo!=null && clubInfo!=null){
                                for (Object o : guotaList) {
                                    JSONObject temp = JSONObject.fromObject(o);
                                    if (temp!=null){
                                        int gcid = Integer.parseInt(temp.get("cid").toString());
                                        if (unionInfo.getCreator_clubid() == clubInfo.getId()){
                                            if (gcid==cid){
                                                minlogStruct.setFenpei(Integer.parseInt(temp.get("quota").toString()));
                                            }
                                        }else{
                                            if (gcid==cid && cid == clubInfo.getId()){
                                                minlogStruct.setFenpei(Integer.parseInt(temp.get("quota").toString()));
                                            }
                                        }


                                    }
                                }

                            }
                        }
                        if (minlogStruct.getFenpei()>0){
                            XyClubInfo toinfo = DataManager.getInstance().getMapClub().get(cid);
                            minlogStruct.setFenpeiclubName(toinfo.getName());
                        }
                    }
                    minlogStructs[index] = minlogStruct;

                    zongdairu+=dairu;
                    zongshoushu+=shoushu;
                    clubfenpei+=minlogStruct.getFenpei();
                    int jifen = 0;
                    int clubdairu = 0;
                    XyUnionInfo unionInfo = DataManager.getInstance().getMapUnion().get(this.unionid);
                    XyClubInfo clubInfo = DataManager.getInstance().getMapClub().get(this.clubid);
                    if (this.type==2){
                        if (unionInfo!=null && clubInfo!=null){
                            if (unionInfo.getCreator_clubid() == clubInfo.getId()){
                                clubdairu = dairu;
                                jifen = Math.abs(win);
                            }else{
                                if (this.clubid==cid){
                                    clubdairu = dairu;
                                    jifen = Math.abs(win);
                                }
                            }
                        }
                    }
                    clubzongdairu+=clubdairu;
                    clubjifen+=jifen;
                    index++;
                }
                rsp.setZongdairu(zongdairu);
                rsp.setClubdairu(clubzongdairu);
                rsp.setClubfenpei(clubfenpei);
                rsp.setClubjifen(clubjifen);
                rsp.setGameminlogs(minlogStructs);
                rsp.setShoushu(zongshoushu);
                if (this.type==2 && tabinfo.getInt("clubid")!=0){
                    XyClubInfo clubInfo = DataManager.getInstance().getMapClub().get(tabinfo.getInt("clubid"));
                    rsp.setCname(clubInfo.getName());
                }
                client.Send(rsp);
            }
        }
    }

}
